package com.aoasaa;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import com.aoasaa.UserCustomSearching.MyAsyncTask123;
import com.userutils.WSConnectionFile;
import com.userutils.WSConnectionStrings;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.Toast;

public class ViewingAllNotes extends Activity 
{ 
	public SharedPreferences sp_obj;
	public Editor editor_obj;
	String note_type=""; 
	String notes_url="",user_notes=""; 
	ImageView img;
	@Override   
    public void onCreate(Bundle savedInstanceState) 
	{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewingnotesfile);  
        // accessing shared preference     
 		sp_obj=getSharedPreferences("ArchiveApp",MODE_WORLD_WRITEABLE);
 		editor_obj=sp_obj.edit();
 		
 		
        // creating reference for linear layout
 		note_type=sp_obj.getString("retrivednote_type", "NOA");
        user_notes=sp_obj.getString("retrivedContent", "NOA");
        notes_url=sp_obj.getString("retrivedurl", "NOA");
        LinearLayout ll_viewnotes=(LinearLayout)findViewById(R.id.layout_viewingallnotes);
        
        // creating views dynamically
        TableRow tr1=new TableRow(this);
        tr1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        TableRow tr2=new TableRow(this);
        EditText et1=new EditText(this);
        if(note_type.equals("text"))
        {
        	// adding notes  
            et1.setText(user_notes);
            tr1.addView(et1);
            ll_viewnotes.addView(tr1);
        }
        else if(note_type.equals("image"))
        {
        	// adding notes
            
        	et1.setText(user_notes);
            tr1.addView(et1);
            
            // adding images
             
            img =new ImageView(this); 
            tr2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
            tr2.addView(img);
            ll_viewnotes.addView(tr1); 
            ll_viewnotes.addView(tr2);
            new MyAsyncTask123().execute();
                
        }
        else if(note_type.equals("voice"))   
        {
        	// adding button for listening voice
        	Button btn_voicenote=new Button(this);
        	btn_voicenote.setText("Click to listen Voice note");
        	tr1.addView(btn_voicenote); 
        	ll_viewnotes.addView(tr1);    
        	btn_voicenote.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Toast.makeText(ViewingAllNotes.this, notes_url, Toast.LENGTH_LONG).show();
				Intent i=new Intent(ViewingAllNotes.this,UserPlayingAudioFiles.class);
				startActivity(i); 
				}   
			});
        }
        
         
	}
	// inner class  For Network Connection i.e, for Web service connection
	class MyAsyncTask123 
	extends AsyncTask<Void, Bitmap, Bitmap> 
	{
		@Override    
		protected void onPostExecute(Bitmap response) 
		{
			// LOGIC for displaying image from URL
			try
			{
				img.setImageBitmap(response); 
			}
			catch (Exception e)  
			{  
				e.printStackTrace();
			}
		}
		@Override 
		protected Bitmap doInBackground(Void... params) {
			// your network operation
			Bitmap bmp = null;
			try
            {
           	URL aURL = new URL(notes_url);
    			URLConnection conn = aURL.openConnection();
    			conn.connect();
 
    			InputStream is=conn.getInputStream();
    			BufferedInputStream bis=new BufferedInputStream(is);
    	        bmp=BitmapFactory.decodeStream(bis);
    			bis.close();
    			is.close();
    			//img.setImageBitmap(bmp);
            }   
            catch (Exception e) 
            {
			  e.printStackTrace();
			 }    
			return bmp; 
		}        
	}
}
